import { Context } from 'koa';
const { getVideos, getVideosByPage, getVideoById, createCourseVideo, update, deleteVideo } = require('../service/course_video.service')
const { isEmpty, invalidVideoId } = require('../constants/err.type')

interface pageInfo {
    pageNum?: number,
    pageSize?: number
}

class CourseVideoController {
    // 获取课程视频列表接口
    public async getCourseVideo(ctx: Context) {
        try {
            let res: any = null;
            let { pageNum, pageSize }: pageInfo = ctx.query;
            if (pageNum && pageSize) {
                pageNum = Math.max(pageNum * 1, 1);
                pageSize = Math.max(pageSize * 1, 1);
                res = await getVideosByPage(pageNum, pageSize);
            } else {
                res = await getVideos();
            }
            ctx.body = {
                code: 200,
                message: '获取课程视频列表成功',
                result: {
                    ...res
                }
            }
        } catch (error) {
            console.error('获取课程视频列表失败', error)
        }
    }
    // 根据id获取课程视频
    public async getById(ctx: Context) {
        try {
            const id = ctx.params.id
            const res: any = await getVideoById(id);
            if(res) {
                ctx.body = {
                    code: 200,
                    message: '根据id获取课程视频成功',
                    result: res
                }
            } else {
                return ctx.app.emit('error', invalidVideoId, ctx)
            }
        } catch (error) {
            console.error('根据id获取课程视频失败', error)
        }
    }
    // 添加课程视频
    public async addCourseVideo(ctx: Context) {
        try {
            const res = await createCourseVideo(ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `添加课程视频成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `添加课程视频失败`
                }
            }
        } catch (error) {
            console.error('添加课程视频失败', error)
        }
    }
    // 更新课程视频
    public async updateCourseVideo(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await update(id, ctx.request.body)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `更新课程视频成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `更新课程视频失败`
                }
            }
        } catch (error) {
            console.error('更新课程视频视频', error)
        }
    }
    // 删除课程视频
    public async deleteCourseVideo(ctx: Context) {
        try {
            const id = ctx.params.id
            const res = await deleteVideo(id)
            if(res) {
                ctx.body = {
                    code: 200,
                    message: `删除课程视频成功`
                }
            } else {
                ctx.body = {
                    code: 500,
                    message: `删除课程视频失败`
                }
            }
        } catch (error) {
            console.error('更新课程视频失败', error)
        }
    }
}

module.exports = new CourseVideoController();